package com.java1234.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java1234.entity.PageBean;
import com.java1234.entity.R;
import com.java1234.entity.ProjectProgress;
import com.java1234.service.ProjectProgressService;
import com.java1234.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 项目进度汇报表 前端控制器
 * </p>
 *
 * @author zhai
 * @since 2025-05-10
 */
@RestController
@RequestMapping("/sys/projectProgress")
public class ProjectProgressController {
    @Autowired
    private ProjectProgressService service;

    @PostMapping("/save")
//    @PreAuthorize("hasAuthority('system:bumen:save')")
    public R save(@RequestBody ProjectProgress entity){
        entity.setScoreMonth(new Date().getMonth()+1);// 月份从0开始，所以要加1
        if(entity.getId()==null || entity.getId()==-1){
            if (service.checkMonthlySubmission(entity.getUserid(), entity.getScoreMonth())) {
                return R.error("本月已提交过进度汇报，无法重复提交");
            }
            service.save(entity);
        }else{
            service.updateById(entity);
        }
        return R.ok();
    }

    @PostMapping("/list")
//    @PreAuthorize("hasAuthority('system:bumen:list')")
    public R list(@RequestBody PageBean pageBean){
        String query = pageBean.getQuery() != null ? pageBean.getQuery().trim() : "";
        String query2 = pageBean.getQuery2() != null ? pageBean.getQuery2().trim() : "";
        String query3 = pageBean.getQuery3() != null ? pageBean.getQuery3().trim() : "";
        Page<ProjectProgress> pageResult = service.page(
                new Page<>(pageBean.getPageNum(), pageBean.getPageSize()),
                new QueryWrapper<ProjectProgress>()
                        .like(StringUtil.isNotEmpty(query), "report_content", query)
                        .eq(StringUtil.isNotEmpty(query2), "score_month", query2)
                        .eq(StringUtil.isNotEmpty(query3), "username", query3)
                        .orderByDesc("id") // 改为查询bumen字段而不是username
        );
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("list",pageResult.getRecords());
        resultMap.put("total",pageResult.getTotal());
        return R.ok(resultMap);
    }

    @PostMapping("/delete")
    @Transactional
//    @PreAuthorize("hasAuthority('system:bumen:delete')")
    public R delete(@RequestBody Long[] ids){
        service.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/info/{id}") // 添加这个接口匹配前端请求
//    @PreAuthorize("hasAuthority('system:bumen:query')")
    public R info(@PathVariable(value = "id") Long id){
        ProjectProgress entity = service.getById(id);
        Map<String,Object> map=new HashMap<>();
        map.put("bumen",entity); // 改为"bumen"匹配前端期望
        return R.ok(map);
    }
}
